package manager

import (
	"errors"
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/request"
	"gitcode.com/G-YT/iHealth25/server/model/manager"
	managerReq "gitcode.com/G-YT/iHealth25/server/model/manager/request"
	"gitcode.com/G-YT/iHealth25/server/model/system"
	"gitcode.com/G-YT/iHealth25/server/utils"
	"gorm.io/gorm"
	"time"
)

type FdHealthManagerAuditService struct {
}

// CreateFdHealthManagerAudit 创建健康师注册记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdHealthManagerAuditService *FdHealthManagerAuditService) CreateFdHealthManagerAudit(userID uint) (err error) {
	var audit manager.FdHealthManagerAudit
	_ = global.GVA_DB.Order("id desc").First(&audit, "hm_id = ?", userID).Error
	if audit.AuditStatus != nil && *audit.AuditStatus == 1 {
		return errors.New("该用户已审核成功")
	}
	if audit.AuditStatus != nil && *audit.AuditStatus == 0 {
		return errors.New("该用户正在审核中")
	}
	fdHealthManagerAudit := manager.FdHealthManagerAudit{
		HmId:        userID,
		AuditStatus: utils.Pointer(0),
	}
	err = global.GVA_DB.Create(&fdHealthManagerAudit).Error
	return err
}

// DeleteFdHealthManagerAudit 删除健康师注册记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdHealthManagerAuditService *FdHealthManagerAuditService) DeleteFdHealthManagerAudit(fdHealthManagerAudit manager.FdHealthManagerAudit) (err error) {
	err = global.GVA_DB.Delete(&fdHealthManagerAudit).Error
	return err
}

// DeleteFdHealthManagerAuditByIds 批量删除健康师注册记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdHealthManagerAuditService *FdHealthManagerAuditService) DeleteFdHealthManagerAuditByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]manager.FdHealthManagerAudit{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateFdHealthManagerAudit 更新健康师注册记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdHealthManagerAuditService *FdHealthManagerAuditService) UpdateFdHealthManagerAudit(fdHealthManagerAudit manager.FdHealthManagerAudit) (err error) {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if fdHealthManagerAudit.AuditStatus != nil && *fdHealthManagerAudit.AuditStatus == 1 {
			fdHealthManagerAudit.AuditInfo = "通过"
			err = global.GVA_DB.First(&manager.FdHealthManager{}, "hm_id = ?", fdHealthManagerAudit.HmId).Update("is_audit", true).Update("created_at", time.Now()).Error
			if err != nil {
				return err
			}
		}
		err = global.GVA_DB.Save(&fdHealthManagerAudit).Error
		return err
	})
}

// GetFdHealthManagerAudit 根据id获取健康师注册记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdHealthManagerAuditService *FdHealthManagerAuditService) GetFdHealthManagerAudit(id uint) (fdHealthManagerAudit manager.FdHealthManagerAudit, err error) {
	err = global.GVA_DB.Preload("FdHealthManager").Where("id = ?", id).First(&fdHealthManagerAudit).Error
	return
}

// GetFdHealthManagerAuditInfoList 分页获取健康师注册记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdHealthManagerAuditService *FdHealthManagerAuditService) GetFdHealthManagerAuditInfoList(info managerReq.FdHealthManagerAuditSearch) (list []manager.FdHealthManagerAudit, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Where("store_id = ?", info.FdHealthManager.StoreId).Joins("FdHealthManager").Preload("FdHealthManager", func(db *gorm.DB) *gorm.DB {
		return db.Omit("Password")
	}).Model(&manager.FdHealthManagerAudit{})
	var fdHealthManagerAudits []manager.FdHealthManagerAudit
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("FdHealthManager.name LIKE ?", "%"+info.Name+"%")
	}
	if info.Phone != "" {
		db = db.Where("FdHealthManager.phone = ?", info.Phone)
	}
	if info.AuditStatus != nil {
		db = db.Where("FdHealthManager.is_audit = ?", info.AuditStatus)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&fdHealthManagerAudits).Error
	return fdHealthManagerAudits, total, err
}

// GetScHealthManagerAuditList 获取健康师注册列表【服务中心】
// Author [g-qs](https://gitee.com/g-qs)
func (fdHealthManagerAuditService *FdHealthManagerAuditService) GetScHealthManagerAuditList(info managerReq.FdHealthManagerAuditSearch, ScID uint) (list []manager.FdHealthManagerAudit, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	var serviceStations []system.SysUser
	err = global.GVA_DB.Model(&system.SysUser{}).Where("creator = ?", ScID).Find(&serviceStations).Error
	var ids []uint
	for _, v := range serviceStations {
		ids = append(ids, v.ID)
	}
	db := global.GVA_DB.Where("store_id IN (?)", ids).Joins("FdHealthManager").Preload("FdHealthManager", func(db *gorm.DB) *gorm.DB {
		return db.Omit("Password")
	}).Model(&manager.FdHealthManagerAudit{})
	var fdHealthManagerAudits []manager.FdHealthManagerAudit
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("FdHealthManager.name LIKE ?", "%"+info.Name+"%")
	}
	if info.Phone != "" {
		db = db.Where("FdHealthManager.phone = ?", info.Phone)
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&fdHealthManagerAudits).Error
	return fdHealthManagerAudits, total, err
}

// GetAllHealthManagerAuditInfoList 分页获取全部健康师注册记录【超管】
// Author [g-qs](https://gitee.com/g-qs)
func (fdHealthManagerAuditService *FdHealthManagerAuditService) GetAllHealthManagerAuditInfoList(info managerReq.FdHealthManagerAuditSearch) (list []manager.FdHealthManagerAudit, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Joins("FdHealthManager").Preload("FdHealthManager", func(db *gorm.DB) *gorm.DB {
		return db.Preload("HealthManageCenter").Omit("Password")
	}).Model(&manager.FdHealthManagerAudit{})
	var fdHealthManagerAudits []manager.FdHealthManagerAudit
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("FdHealthManager.created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("FdHealthManager.name LIKE ?", "%"+info.Name+"%")
	}
	if info.Phone != "" {
		db = db.Where("FdHealthManager.phone = ?", info.Phone)
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&fdHealthManagerAudits).Error
	return fdHealthManagerAudits, total, err
}
